package com.jeeplus.common;
import java.io.*;

import com.aspose.cells.License;
import com.aspose.cells.Workbook;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.FileUtils;

/**
 * @description: word/excel转为pdf
 * @create: 2019-05-13 16:35
 **/
public class Excel2PdfUtil {

    public static final String pdfPath = Global.getUserfilesBaseDir()+"/userfiles/PDF/";
    public static final String excelPath = Global.getUserfilesBaseDir()+"/userfiles/excel/";
    public static final String modelPath = Global.getUserfilesBaseDir()+"/userfiles/model/xxjy/";
    public static final String hc = Global.getUserfilesBaseDir()+"/userfiles/hc/";
    public static final String fj = Global.getUserfilesBaseDir();

    /**
     * 校验license
     * @return
     */
    private static boolean getLicense() {
        boolean result = false;
        try {
            InputStream inputStream = Excel2PdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(inputStream);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @param excelName 需要被转换的excel文件名
     */
    public static void excel2pdf(String excelName) {
        // 验证License
        if (!getLicense()) {
            return;
        }
        try {
            // 原始excel路径
            Workbook wb = new Workbook(excelPath+excelName);
            FileOutputStream fileOS = new FileOutputStream(new File(pdfPath+excelName.split("\\.")[0]+".pdf"));
            wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);
            fileOS.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取文件数组
     * @param folder
     * @return
     * @throws IOException
     */
    public static String[] getFiles(String folder) throws IOException {
        File _folder = new File(folder);
        String[] filesInFolder;

        if (_folder.isDirectory()) {
            filesInFolder = _folder.list();
            return filesInFolder;
        } else {
            throw new IOException("路径不对");
        }
    }

    public static String mergePdf(){
        Document document = null;
        PdfReader pdfReader = null;
        PdfCopy copy = null;
        FileOutputStream outputStream = null;
        String pdfName = "hc" + DateUtils.getDate("yyyyMMddHHmmss") + ".pdf";
        String path = hc + pdfName;
        try {
            String[] files = getFiles(pdfPath);
            pdfReader = new PdfReader(pdfPath+files[0]);
            document = new Document(pdfReader.getPageSize(1));
            outputStream = new FileOutputStream(path);
            copy = new PdfCopy(document, outputStream);
            document.open();
            for (int i = 0; i < files.length; i++) {
                PdfReader reader = new PdfReader(Excel2PdfUtil.pdfPath+files[i]);
                int n = reader.getNumberOfPages();
                for (int j = 1; j <= n; j++) {
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                }
                reader.close();
            }
            document.close();
            copy.close();
            outputStream.close();
            pdfReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            FileUtils.deleteDirectory(pdfPath);
            FileUtils.deleteDirectory(excelPath);
            return pdfName;
        }
    }

    // public static void main(String[] args) {

        // String  excelPdfPath = "D:/收款单1.pdf";
        // String  excel2pdf = "D:/收款单20191111024845.xlsx";
        // excel2pdf(excel2pdf, excelPdfPath);


    // }
}

